/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.publish.service.impl;

import me.zhengjie.modules.publish.domain.PublishType;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.publish.repository.PublishTypeRepository;
import me.zhengjie.modules.publish.service.PublishTypeService;
import me.zhengjie.modules.publish.service.dto.PublishTypeDto;
import me.zhengjie.modules.publish.service.dto.PublishTypeQueryCriteria;
import me.zhengjie.modules.publish.service.mapstruct.PublishTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
*
* @description 服务实现
* @author majinhui
* @date 2020-09-08
**/
@Service
@RequiredArgsConstructor
public class PublishTypeServiceImpl implements PublishTypeService {

    private final PublishTypeRepository publishTypeRepository;
    private final PublishTypeMapper publishTypeMapper;

    @Override
    public Map<String,Object> queryAll(PublishTypeQueryCriteria criteria, Pageable pageable){
        Page<PublishType> page = publishTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(publishTypeMapper::toDto));
    }

    @Override
    public List<PublishTypeDto> queryAll(PublishTypeQueryCriteria criteria){
        return publishTypeMapper.toDto(publishTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PublishTypeDto findById(Integer id) {
        PublishType publishType = publishTypeRepository.findById(id).orElseGet(PublishType::new);
        ValidationUtil.isNull(publishType.getId(),"PublishType","id",id);
        return publishTypeMapper.toDto(publishType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PublishTypeDto create(PublishType resources) {
        return publishTypeMapper.toDto(publishTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PublishType resources) {
        PublishType publishType = publishTypeRepository.findById(resources.getId()).orElseGet(PublishType::new);
        ValidationUtil.isNull( publishType.getId(),"PublishType","id",resources.getId());
        publishType.copy(resources);
        publishTypeRepository.save(publishType);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            publishTypeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PublishTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PublishTypeDto publishType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发布类型描述", publishType.getName());
            map.put("对应前端url", publishType.getFrontUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}